"""
Case Type   : GSC功能模块
Case Name   : 异步备全局系统缓存更新验证
Create At   : 2024/10/26
Owner       : @lonely-dance
Description :
    1.配置数据库参数
    2.重启数据库生效
    3.主机循环执行alter table操作
    4.备机执行循环查询操作
    5.清理环境
Expect      :
    1.修改参数成功
    2.重启成功
    3.执行成功
    4.查寻成功
    5.成功
History     :
"""
import os
import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

pri_sh = CommonSH('PrimaryDbUser')
node_num = pri_sh.get_node_num()


@unittest.skipIf(node_num < 3, '非1主2备环境不执行')
class GscTestCase(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.standby1_sh = CommonSH('Standby1DbUser')
        self.tb_name = 'tb_gsc0059'
        self.com = Common('PrimaryDbUser')
        self.constant = Constant()
        self.p_node = Node('PrimaryDbUser')
        self.init_para1 = None
        self.init_para2 = None

    def test_GSC(self):
        text = '----step1:配置数据库参数;----'
        self.log.info(text)
        status_result = self.pri_sh.get_db_cluster_status('status')
        self.assertTrue(status_result, '执行失败:' + text)
        text = '----查看初始配置值;----'
        self.log.info(text)
        self.init_para1 = self.com.show_param('enable_global_syscache')
        self.init_para2 = self.com.show_param('synchronous_commit')
        self.log.info(self.init_para1)
        self.log.info(self.init_para2)

        result = self.pri_sh.execute_gsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           f"enable_global_syscache=on")
        self.log.info(result)
        self.assertTrue(result, '执行失败:' + text)

        result = self.pri_sh.execute_gsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           f"synchronous_commit=off")
        self.assertTrue(result, '执行失败:' + text)

        text = '----step2:重启数据库生效; expect:重启成功----'
        self.log.info(text)
        restart = self.pri_sh.restart_db_cluster()
        self.assertTrue(restart, '执行失败:' + text)

        text = f'----step3:主机循环执行alter table操作; expect:执行成功----'
        self.log.info(text)
        cmd = f"""
            drop table if exists {self.tb_name};
            create table {self.tb_name}(id int,name text);';
        """
        self.log.info(cmd)
        res = self.pri_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res, '执行失败:' + text)

        cmd = f"alter table {self.tb_name} add column bak text;" \
            f"alter table {self.tb_name} drop column bak ;"
        self.log.info(cmd)
        thread_1 = ComThread(self.pri_sh.cycle_exec_sql, args=(cmd, 1000, False, None, True))
        thread_1.setDaemon(True)
        thread_1.start()


        text = '----step4:备机循环查询table; expect:查询成功----'
        self.log.info(text)
        cmd = f"select * from {self.tb_name};"
        self.log.info(cmd)
        thread_2 = ComThread(self.standby1_sh.cycle_exec_sql, args=(cmd, 500, False, None, True))
        thread_2.setDaemon(True)
        thread_2.start()

        thread_1.join(300)
        thread_2.join(300)
        res1 = thread_1.get_result()
        res2 = thread_2.get_result()
        self.log.info(res1)
        self.log.info(res2)
        self.assertTrue('ERROR' not in res1, '执行失败:' + text)
        self.assertTrue('ERROR' not in res2, '执行失败:' + text)

    def tearDown(self):
        text = '----step5:清理环境; expect:成功----'
        self.log.info(text)
        cmd = f"""
            drop table if exists {self.tb_name};
        """
        res = self.pri_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res, '执行失败:' + text)

        text = '----恢复数据库参数----'
        self.log.info(text)
        result = self.pri_sh.execute_gsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           f"enable_global_syscache={self.init_para1}")
        self.assertTrue(result, '执行失败:' + text)

        result = self.pri_sh.execute_gsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           f"synchronous_commit={self.init_para2}")
        self.assertTrue(result, '执行失败:' + text)

        text = '----重启数据库----'
        self.log.info(text)
        restart = self.pri_sh.restart_db_cluster()
        self.assertTrue(restart, '执行失败:' + text)

        self.log.info(f'----{os.path.basename(__file__)} end----')
